package com.micro.modual.extension;

import com.alibaba.fastjson.JSON;
import com.micro.common.MicroConstant;
import com.micro.common.biz.GeneratorCodeUtil;
import com.micro.common.util.SnowFlakeUtil;
import com.micro.core.handler.BaseContextHandler;
import com.micro.modual.entity.CouponTemplate;
import com.micro.modual.model.bo.coupontemplate.CouponTemplateBO;
import com.micro.modual.model.dto.coupontemplate.CouponTemplateDetailDTO;
import com.micro.modual.model.dto.coupontemplate.CouponTemplateListDTO;
import com.micro.modual.model.vo.TemplateRule;
import org.apache.commons.collections.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <h1>优惠券模版表 延展类</h1>
 *
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:23:27
 */
public class CouponTemplateExtension {

    /**
     * <h2>转换: 优惠券模版表 新增</h2>
     *
     * @param couponTemplateBO {@link CouponTemplateBO} 优惠券模版表 新增/修改 请求对象
     * @return {@link CouponTemplate} 优惠券模版表
     */
    public static CouponTemplate convertAddCouponTemplate(CouponTemplateBO couponTemplateBO) {
        CouponTemplate couponTemplate = new CouponTemplate();
        couponTemplate.setId(SnowFlakeUtil.getId());
        couponTemplate.setProductLine(couponTemplateBO.getProductLine());
        couponTemplate.setExpired((byte)2);
        couponTemplate.setCreateTime(new Date());
        couponTemplate.setRule(JSON.toJSONString(couponTemplateBO.getRule()));
        couponTemplate.setAvailable((byte)2);
        couponTemplate.setCouponCount(couponTemplateBO.getCount());
        couponTemplate.setTarget(couponTemplateBO.getTarget());
        couponTemplate.setUserCode(BaseContextHandler.getCustomerCode());
        couponTemplate.setTemplateCategory(couponTemplateBO.getCategory());
        couponTemplate.setTemplateIntro(couponTemplateBO.getDesc());
        couponTemplate.setTemplateName(couponTemplateBO.getName());
        couponTemplate.setTemplateLogo(couponTemplateBO.getLogo());
        //4(产品线和类型)+8(日期yyyyMMdd)
        StringBuffer sb = new StringBuffer();
        sb.append(couponTemplateBO.getProductLine()).append(couponTemplateBO.getCategory()).append(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        couponTemplate.setTemplateKey(sb.toString());
        //优惠券模版编号
        couponTemplate.setTemplateCode(GeneratorCodeUtil.generatorCouponTemplateCode(String.valueOf(couponTemplateBO.getProductLine()),couponTemplateBO.getCategory()));
        return couponTemplate;
    }

    /**
     * <h2>转换: 优惠券模版表 修改</h2>
     *
     * @param couponTemplateBO {@link CouponTemplateBO} 优惠券模版表 新增/修改 请求对象
     * @return {@link CouponTemplate} 优惠券模版表
     */
    public static CouponTemplate convertEditCouponTemplate(CouponTemplateBO couponTemplateBO) {
        CouponTemplate couponTemplate = new CouponTemplate();
        couponTemplate.setId(couponTemplateBO.getId());
        couponTemplate.setProductLine(couponTemplateBO.getProductLine());
        couponTemplate.setRule(JSON.toJSONString(couponTemplateBO.getRule()));
        couponTemplate.setCouponCount(couponTemplateBO.getCount());
        couponTemplate.setTarget(couponTemplateBO.getTarget());
        couponTemplate.setTemplateCategory(couponTemplateBO.getCategory());
        couponTemplate.setTemplateIntro(couponTemplateBO.getDesc());
        couponTemplate.setTemplateName(couponTemplateBO.getName());
        couponTemplate.setTemplateLogo(couponTemplateBO.getLogo());
        return couponTemplate;
    }


    /**
     * <h2>转换优惠券模版对象</h2>
     * @param couponTemplateList
     * @return
     */
    public static List<CouponTemplateListDTO> convertCouponTemplateListDTO(List<CouponTemplate> couponTemplateList) {
        List<CouponTemplateListDTO> couponTemplateListDTOList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(couponTemplateList)){
            for(CouponTemplate couponTemplate:couponTemplateList){
                CouponTemplateListDTO couponTemplateListDTO = new CouponTemplateListDTO();
                couponTemplateListDTO.setId(couponTemplate.getId());
                couponTemplateListDTO.setAvailable(couponTemplate.getAvailable());
                couponTemplateListDTO.setCouponCount(couponTemplate.getCouponCount());
                couponTemplateListDTO.setCreateTime(couponTemplate.getCreateTime());
                couponTemplateListDTO.setExpired(couponTemplate.getExpired());
                couponTemplateListDTO.setProductLine(couponTemplate.getProductLine());
                couponTemplateListDTO.setRule(JSON.parseObject(couponTemplate.getRule(), TemplateRule.class));
                couponTemplateListDTO.setTarget(couponTemplate.getTarget());
                couponTemplateListDTO.setTemplateCategory(couponTemplate.getTemplateCategory());
                couponTemplateListDTO.setTemplateCode(couponTemplate.getTemplateCode());
                couponTemplateListDTO.setTemplateName(couponTemplate.getTemplateName());
                couponTemplateListDTO.setTemplateLogo(couponTemplate.getTemplateLogo());
                couponTemplateListDTO.setTemplateKey(couponTemplate.getTemplateKey());
                couponTemplateListDTO.setTemplateIntro(couponTemplate.getTemplateIntro());
                couponTemplateListDTO.setUserCode(couponTemplate.getUserCode());
                couponTemplateListDTOList.add(couponTemplateListDTO);
            }
        }
        return couponTemplateListDTOList;
    }

    /**
     * <h2>转换优惠券模版对象</h2>
     * @param couponTemplate
     * @return
     */
    public static CouponTemplateDetailDTO convertCouponTemplateDetailDTO(CouponTemplate couponTemplate) {
        CouponTemplateDetailDTO couponTemplateDetailDTO = new CouponTemplateDetailDTO();
        couponTemplateDetailDTO.setId(couponTemplate.getId());
        couponTemplateDetailDTO.setAvailable(couponTemplate.getAvailable());
        couponTemplateDetailDTO.setCount(couponTemplate.getCouponCount());
        couponTemplateDetailDTO.setCreateTime(couponTemplate.getCreateTime());
        couponTemplateDetailDTO.setExpired(couponTemplate.getExpired());
        couponTemplateDetailDTO.setProductLine(couponTemplate.getProductLine());
        couponTemplateDetailDTO.setRule(JSON.parseObject(couponTemplate.getRule(), TemplateRule.class));
        couponTemplateDetailDTO.setTarget(couponTemplate.getTarget());
        couponTemplateDetailDTO.setCategory(couponTemplate.getTemplateCategory());
        couponTemplateDetailDTO.setTemplateCode(couponTemplate.getTemplateCode());
        couponTemplateDetailDTO.setName(couponTemplate.getTemplateName());
        couponTemplateDetailDTO.setLogo(couponTemplate.getTemplateLogo());
        couponTemplateDetailDTO.setTemplateKey(couponTemplate.getTemplateKey());
        couponTemplateDetailDTO.setDesc(couponTemplate.getTemplateIntro());
        couponTemplateDetailDTO.setUserCode(couponTemplate.getUserCode());
        return couponTemplateDetailDTO;
    }


}
